package com.erp.sd.service.impl;

import com.erp.sd.entity.TSdSoHeadEntity;
import com.erp.sd.entity.TSdSoItemEntity;
import com.erp.sd.service.TSdSoHeadServiceI;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


@Service("tSdSoHeadService")
@Transactional
public class TSdSoHeadServiceImpl extends CommonServiceImpl implements TSdSoHeadServiceI {
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((TSdSoHeadEntity)entity);
 	}
	
	public void addMain(TSdSoHeadEntity tSdSoHead,
	        List<TSdSoItemEntity> tSdSoItemList){
			//保存主信息
			this.save(tSdSoHead);
		
			/**保存-销售订单项目*/
			for(TSdSoItemEntity tSdSoItem:tSdSoItemList){
				//外键设置
				tSdSoItem.setSoid(tSdSoHead.getId());
				this.save(tSdSoItem);
			}
			//执行新增操作配置的sql增强
 			this.doAddSql(tSdSoHead);
	}

	
	public void updateMain(TSdSoHeadEntity tSdSoHead,
	        List<TSdSoItemEntity> tSdSoItemList) {
		//保存主表信息
		this.saveOrUpdate(tSdSoHead);
		//===================================================================================
		//获取参数
		Object id0 = tSdSoHead.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-销售订单项目
	    String hql0 = "from TSdSoItemEntity where 1 = 1 AND sOID = ? ";
	    List<TSdSoItemEntity> tSdSoItemOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-销售订单项目
		if(tSdSoItemList!=null&&tSdSoItemList.size()>0){
		for(TSdSoItemEntity oldE:tSdSoItemOldList){
			boolean isUpdate = false;
				for(TSdSoItemEntity sendE:tSdSoItemList){
					//需要更新的明细数据-销售订单项目
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-销售订单项目
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-销售订单项目
			for(TSdSoItemEntity tSdSoItem:tSdSoItemList){
				if(oConvertUtils.isEmpty(tSdSoItem.getId())){
					//外键设置
					tSdSoItem.setSoid(tSdSoHead.getId());
					this.save(tSdSoItem);
				}
			}
		}
		//执行更新操作配置的sql增强
 		this.doUpdateSql(tSdSoHead);
	}

	
	public void delMain(TSdSoHeadEntity tSdSoHead) {
		//删除主表信息
		this.delete(tSdSoHead);
		//===================================================================================
		//获取参数
		Object id0 = tSdSoHead.getId();
		//===================================================================================
		//删除-销售订单项目
	    String hql0 = "from TSdSoItemEntity where 1 = 1 AND sOID = ? ";
	    List<TSdSoItemEntity> tSdSoItemOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(tSdSoItemOldList);
	}
	
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(TSdSoHeadEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(TSdSoHeadEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(TSdSoHeadEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,TSdSoHeadEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
 		sql  = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
 		sql  = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
 		sql  = sql.replace("#{docnum}",String.valueOf(t.getDocnum()));
 		sql  = sql.replace("#{customercode}",String.valueOf(t.getCustomercode()));
 		sql  = sql.replace("#{customername}",String.valueOf(t.getCustomername()));
 		sql  = sql.replace("#{docdate}",String.valueOf(t.getDocdate()));
 		sql  = sql.replace("#{text}",String.valueOf(t.getText()));
 		sql  = sql.replace("#{hb_code}",String.valueOf(t.getHbCode()));
 		sql  = sql.replace("#{sd_bz1}",String.valueOf(t.getSdBz1()));
 		sql  = sql.replace("#{sd_bz2}",String.valueOf(t.getSdBz2()));
 		sql  = sql.replace("#{sd_bz3}",String.valueOf(t.getSdBz3()));
 		sql  = sql.replace("#{sd_bz4}",String.valueOf(t.getSdBz4()));
 		sql  = sql.replace("#{sd_bz5}",String.valueOf(t.getSdBz5()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}